﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class Company
    {

        public static int insert(CompanyEntity company)
        {
           return Convert.ToInt32(DbHelper.ExecuteScalar("p_Company_insert",
              company.CompanyNo,
              company.Soruce,
              company.Type,
              company.VipType,
              company.Company,
              company.AreaID,
              company.Address,
              company.Postcode,
              company.Employees,
              company.Contact,
              company.IdCard,
              company.Location,
              company.Mobile,
              company.Phone,
              company.Fax,
              company.Email,
              company.PayMethod,
              company.AccountDay,
              company.buyRange,
              company.cooperateType,
              company.autoPricePercent,
              company.cooperatePercent,
              company.buyerType,
              company.salesperson,
              company.EatPerson,
              company.Score,
              company.ReceiveTime,
              company.EnabledWeeklyPrice,
              company.sellerType,
              company.BusinessNature,
              company.QuotationMode
               ));
        }

        public static void update(CompanyEntity company)
        {
            DbHelper.ExecuteNonQuery("p_Company_update",
              company.CompanyId,
              company.CompanyNo,
              company.Soruce,
              company.Type,
              company.VipType,
              company.Company,
              company.AreaID,
              company.Address,
              company.Postcode,
              company.Employees,
              company.Contact,
              company.IdCard,
              company.Location,
              company.Mobile,
              company.Phone,
              company.Fax,
              company.Email,
              company.PayMethod,
              company.AccountDay,
              company.buyRange,
              company.cooperateType,
              company.autoPricePercent,
              company.cooperatePercent,
              company.buyerType,
              company.salesperson,
              company.EatPerson,
              company.Score,
              company.ReceiveTime,
              company.EnabledWeeklyPrice,
              company.sellerType,
              company.BusinessNature,
              company.QuotationMode
               );
        }

        public static bool updatePartialSeller(CompanyEntity compnay)
        {
            return DbHelper.ExecuteNonQuery("[dbo].[p_Company_UpdatePartial_Seller]",
                compnay.CompanyId, compnay.AreaID, compnay.Address,
                compnay.Location, compnay.Phone, compnay.Fax,
                compnay.Postcode, compnay.Employees, compnay.Contact,
                compnay.IdCard, compnay.Mobile, compnay.Email);
        }

        public static bool updatePartialBuyer(CompanyEntity companyInfo)
        {
            return DbHelper.ExecuteNonQuery("[dbo].[p_Company_UpdatePartial_Buyer]", 
                companyInfo.CompanyId,companyInfo.AreaID,companyInfo.Address,
                companyInfo.Phone,companyInfo.Fax,companyInfo.Postcode,
                companyInfo.EatPerson,companyInfo.Contact,companyInfo.IdCard,
                companyInfo.Mobile,companyInfo.Email);
        }

        //修改审核信息
        public static void updateByCheck(CompanyEntity company)
        {
            DbHelper.ExecuteNonQuery("p_Company_updateByCheck",
              company.CompanyId,           
              company.IsCheck,
              company.AdminId,
              company.Suggest
               );
        }

        public static void delete(int companyId)
        {
            DbHelper.ExecuteNonQuery("p_Company_delete",
                companyId);
        }

        public static CompanyEntity getById(int companyId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_Company_getById", companyId);
            CompanyEntity company = new CompanyEntity();
            if (dr != null)
            {
                company.CompanyId = companyId;
                company.CompanyNo = dr["companyNo"].ToString();
                company.Soruce = dr["soruce"].ToString();
                company.Type = dr["type"].ToString();
                company.VipType = dr["vipType"].ToString();
                company.Company = dr["company"].ToString();
                company.AreaID = Convert.ToInt32( dr["areaID"]);
                company.Address = dr["address"].ToString();
                company.Postcode = dr["postcode"].ToString();
                company.Employees = dr["employees"].ToString();
                company.Contact = dr["contact"].ToString();
                company.IdCard = dr["idCard"].ToString();
                company.Location = dr["location"].ToString();
                company.Mobile = dr["mobile"].ToString();
                company.Phone = dr["phone"].ToString();
                company.Fax = dr["fax"].ToString();
                company.Email = dr["email"].ToString();
                company.PayMethod = dr["payMethod"].ToString();
                if (dr["AccountDay"] != DBNull.Value)
                    company.AccountDay = Convert.ToInt32(dr["AccountDay"]);

                company.Createdt = Convert.ToDateTime(dr["createdt"]);
                company.IsCheck = Convert.ToBoolean(dr["isCheck"]);
                if (dr["AdminId"] != DBNull.Value)
                    company.AdminId = Convert.ToInt32(dr["adminId"]);
                company.Suggest = dr["suggest"].ToString();
                if (dr["checkDt"] != DBNull.Value)
                    company.CheckDt = Convert.ToDateTime(dr["checkDt"]);
                if (dr["buyRange"] != DBNull.Value)
                    company.buyRange = dr["buyRange"].ToString();
             
                company.cooperateType=dr["cooperateType"].ToString();
                if (dr["autoPricePercent"] != DBNull.Value)
                    company.autoPricePercent = Convert.ToDecimal(dr["autoPricePercent"]);
                if (dr["cooperatePercent"] != DBNull.Value)
                    company.cooperatePercent = Convert.ToDecimal(dr["cooperatePercent"]);
                company.buyerType = dr["buyerType"].ToString();
                company.salesperson = dr["salesperson"].ToString();
                company.EatPerson = dr["EatPerson"].ToString();
                if(dr["score"]!=DBNull.Value)
                    company.Score = Convert.ToInt32(dr["Score"]);
                if (dr["receiveTime"] != DBNull.Value)
                    company.ReceiveTime = dr["receiveTime"].ToString();

                if(dr["enabledWeeklyPrice"]!=DBNull.Value)
                    company.EnabledWeeklyPrice = Convert.ToBoolean(dr["enabledWeeklyPrice"]);

                company.sellerType = dr["sellertype"].ToString();
                company.BusinessNature = dr["businessNature"].ToString();
                company.QuotationMode = dr["quotationMode"].ToString();
                
            }
            return company;
        }

        /// <summary>
        /// 返回企业类型表；当前企业类别标识为1
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public static DataTable getType(int companyId)
        {
            return DbHelper.ExecuteDataTable("p_Company_getType", companyId);
        }

        /// <summary>
        /// 查询企业采购范围【已过时】
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public static DataTable getBuyRange(int companyId)
        {
            return DbHelper.ExecuteDataTable("p_Company_getBuyRange", companyId);
        }

        /// <summary>
        /// 查询采购商企业类型（普通或测试）
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public static DataTable getBuyerTypeList(int companyId)
        {
            return DbHelper.ExecuteDataTable("p_Company_getBuyerType", companyId);
        }

        //修改供应商的积分
        public static void updateScore(int companyId,int score)
        {
            DbHelper.ExecuteNonQuery("p_Company_updateScore",companyId,score);
        }

        public static bool isExistCompanyNo(string companyNo)
        {
            if (Convert.ToInt32(DbHelper.ExecuteScalar("p_Company_isExistCompanyNo", companyNo)) == 1)
                return true;
            else
                return false;
        }

        //删除企业
        public static void updateIsDel(CompanyEntity company)
        {
            DbHelper.ExecuteNonQuery("p_Company_updateIsDel", company.CompanyId, company.isDel);
        }

        /// <summary>
        /// 根据企业类型查询列表[销售测试企业除外]
        /// </summary>
        /// <param name="companyType">1表示供应商；2表示采购商</param>
        /// <returns></returns>
        public static DataTable listWithCompanyType(string companyType)
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_Compnay_listWithCompanyType]", companyType);
        }

        /// <summary>
        /// 查询所有采购商企业【包括销售测试企业】
        /// </summary>
        /// <returns></returns>
        public static DataTable listWithCompanyTypeIncludeTest()
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_Compnay_listWithCompanyTypeIncludeTest]");
        }

        /// <summary>
        /// 根据报价模式查询供应商
        /// </summary>
        /// <param name="quotationMode">报价模式 1 表示自由报价模式；2 表示封闭报价模式</param>
        /// <returns></returns>
        public static DataTable listSellerByQuotationMode(string quotationMode)
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_Company_listSellerByQuotationMode]", quotationMode);
        }

        /// <summary>
        /// 查询启用周报价的企业
        /// </summary>
        /// <returns></returns>
        public static DataTable listWithWeeklyPrice()
        {
            return DbHelper.ExecuteDataTable("p_company_listwithWeeklyPrice");
        }

        /// <summary>
        /// 根据年份查询企业周订购量
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public static DataTable listCompanyDataAnalysisByCriteria(int companyId,int year,string mode)
        {
            return DbHelper.ExecuteDataTable("p_Company_DataAnalysis_Criteria", companyId, year,mode);
        }

        /// <summary>
        /// 根据年份查询企业月供应量
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public static DataTable listCompanyDataAnalysisSupplyByCriteria(int companyId, int year)
        {
            return DbHelper.ExecuteDataTable("p_Company_DataAnalysis_Supply_Criteria", companyId, year);
        }

        public static DataTable listCompanyGPMAnalysisByCriteria(int companyId, int year, string mode, DateTime? startDt, DateTime? endDt)
        {
            return DbHelper.ExecuteDataTable("p_Company_GPMAnalysis", companyId, year, mode, startDt, endDt);
        }

        public static void P_CompanyIsdel(int cid)
        {
            DbHelper.ExecuteNonQuery("P_CompanyIsdel",cid);
        }

        public static DataTable listSellerWithSellerRange()
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_company_listSellerWithSupplying]");
        }

        public static bool listCompanyWeekWithByCompanyId(int companyid)
        {
            return (bool) DbHelper.ExecuteScalar("[dbo].[p_company_getByCompanyIdWithWeekCompany]", companyid);
        }

        /// <summary>
        /// 根据ProductId查出有哪几个供应商正在供应
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        public static DataTable listSellerByProductId(int productid)
        {
            return DbHelper.ExecuteDataTable("[dbo].[p_company_listSellerByProductId]",productid);
        }
    }
}
